package com.example.wyemail.MySwing;

import org.w3c.dom.ls.LSOutput;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class MainFrame extends JFrame {

    private JTextField softwarePathField;
    private JTextField uploadFilePathField;

    private JTextField softDataPathField;

    private JTextField exportFilePathField;
    private JLabel fileTypeLabel;



    String softDirectory = "";
    String accountDocument = "";
    String mainDirectory = "";

    String exportDirectory = "";

    public MainFrame() {
        setTitle("支付宝链接");
        setSize(750, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        // 创建组件
        softwarePathField = new JTextField(30);
        JButton selectSoftwareButton = new JButton("选择软件");
        uploadFilePathField = new JTextField(30);
        JButton uploadFileButton = new JButton("上传文件");
        softDataPathField = new JTextField(30);
        JButton softDataButton = new JButton("邮箱数据存储位置");
        exportFilePathField = new JTextField(30);
        JButton exportButton = new JButton("导出位置");
        fileTypeLabel = new JLabel("文件类型: 未知");
        JButton executeButton = new JButton("执行");

        // 添加组件到面板
        JPanel panel = new JPanel();
        panel.setLayout(null);
        //邮箱路径
        JLabel softJL = new JLabel("网易大师邮箱路径:",SwingConstants.RIGHT);
        softJL.setBounds(5,15,200,20);
        panel.add(softJL);
        softwarePathField.setBounds(210,15,300,20);
        panel.add(softwarePathField);
        selectSoftwareButton.setBounds(520,15,150,20);
        panel.add(selectSoftwareButton);
        //数据存储地址
        JLabel dataJL = new JLabel("网易邮箱大师数据存储位置:",SwingConstants.RIGHT);
        dataJL.setBounds(5,40,200,20);
        panel.add(dataJL);
        softDataPathField.setBounds(210,40,300,20);
        panel.add(softDataPathField);
        softDataButton.setBounds(520,40,150,20);
        panel.add(softDataButton);
        //需解析文件路径
        JLabel uploadFileJL = new JLabel("解析邮箱文件路径:",SwingConstants.RIGHT);
        uploadFileJL.setBounds(5,65,200,20);
        panel.add(uploadFileJL);
        uploadFilePathField.setBounds(210,65,300,20);
        panel.add(uploadFilePathField);
        uploadFileButton.setBounds(520,65,150,20);
        panel.add(uploadFileButton);
        //导出数据位置
        JLabel exportJL = new JLabel("导出数据位置:",SwingConstants.RIGHT);
        exportJL.setBounds(5,90,200,20);
        panel.add(exportJL);
        exportFilePathField.setBounds(210,90,300,20);
        panel.add(exportFilePathField);
        exportButton.setBounds(520,90,150,20);
        panel.add(exportButton);
        //执行按钮
        executeButton.setBounds(210,115,300,20);
        panel.add(executeButton);

        // 添加面板到主窗口
        add(panel, BorderLayout.CENTER);
        add(fileTypeLabel, BorderLayout.SOUTH);

        // 添加事件监听器
        selectSoftwareButton.addActionListener(new SelectSoftwareAction());
        uploadFileButton.addActionListener(new UploadFileAction());
        softDataButton.addActionListener(new SelectSoftDataAction());
        exportButton.addActionListener(new ExportFileAction());
        executeButton.addActionListener(new ExecuteAction());
    }

    // 选择软件按钮的事件监听器
    private class SelectSoftwareAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = fileChooser.showOpenDialog(MainFrame.this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                softDirectory = selectedFile.getAbsolutePath();
                System.out.println(softDirectory);
                softwarePathField.setText(selectedFile.getAbsolutePath());
            }
        }
    }

    // 上传文件按钮的事件监听器
    private class UploadFileAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showOpenDialog(MainFrame.this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                uploadFilePathField.setText(selectedFile.getAbsolutePath());
                String fileType = getFileType(selectedFile);
                accountDocument = selectedFile.getAbsolutePath();
                System.out.println(accountDocument);
                fileTypeLabel.setText("文件类型: " + fileType);
            }
        }
    }

    // 选择邮件存储位置(文件夹)按钮的事件监听器
    private class SelectSoftDataAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = fileChooser.showOpenDialog(MainFrame.this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                mainDirectory = selectedFile.getAbsolutePath();
                System.out.println(mainDirectory);
                softDataPathField.setText(selectedFile.getAbsolutePath());
            }
        }

    }

    // 选择导出位置(文件夹)按钮的事件监听器
    private class ExportFileAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = fileChooser.showOpenDialog(MainFrame.this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                exportDirectory = selectedFile.getAbsolutePath();
                System.out.println(exportDirectory);
                exportFilePathField.setText(selectedFile.getAbsolutePath());
            }
        }

    }

    // 获取文件类型的方法
    private String getFileType(File file) {
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex + 1).toLowerCase();
        }
        return "未知";
    }


    // 执行按钮的事件监听器
    private class ExecuteAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String softwarePath = softwarePathField.getText();
            if (!softwarePath.isEmpty()) {
                try {

                    TaskWorker task = new TaskWorker(softDirectory,accountDocument,mainDirectory,exportDirectory);
                    task.execute();
                    JOptionPane.showMessageDialog(MainFrame.this, "程序已执行");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(MainFrame.this, "无法执行程序: " + ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(MainFrame.this, "请先选择一个软件");
            }
        }
    }




    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
}

